﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ItemLoadThread.cs" company="">
//   
// </copyright>
// <summary>
//   The item load thread.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.WinClient.Helper
{
    using System;
    using System.Threading;
    using Common.Comparer;
    using Common.Model;

    /// <summary>
    /// The item load thread.
    /// </summary>
    public class ItemLoadThread : WorkerThreadBase
    {
        public SelectedDatabase selectedDatabase;

        #region Events & delegates

        /// <summary>
        /// The on worker thread event handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The eventargs
        /// </param>
        public delegate void OnWorkerThreadEventHandler(object sender, WorkerThreadEventArgs e);

        /// <summary>
        /// The on start.
        /// </summary>
        public event OnWorkerThreadEventHandler OnStart;

        /// <summary>
        /// The on finish.
        /// </summary>
        public event OnWorkerThreadEventHandler OnFinish;

        /// <summary>
        /// The on failed.
        /// </summary>
        public event OnWorkerThreadEventHandler OnFailed;

        /// <summary>
        /// The on success.
        /// </summary>
        public event OnWorkerThreadEventHandler OnSuccess;
        #endregion Events & delegates

        #region Fields & properties

        /// <summary>
        /// The _db objects.
        /// </summary>
        private readonly DatabaseInfoBase dbObjects = new DatabaseInfoBase();

        /// <summary>
        /// The _compare options.
        /// </summary>
        private readonly SqlCompareOptions _compareOptions;

        /// <summary>
        /// The _only exists.
        /// </summary>
        private readonly bool _onlyExists;

        /// <summary>
        /// The _db info.
        /// </summary>
        private readonly DatabaseObjects _dbInfo;
        #endregion Fields & properties

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemLoadThread"/> class.
        /// </summary>
        /// <param name="dbInfo">
        /// The db info.
        /// </param>
        /// <param name="Obj">
        /// The obj.
        /// </param>
        /// <param name="sqloption">
        /// The sqloption.
        /// </param>
        /// <param name="onlyExists">
        /// The only exists.
        /// </param>
        public ItemLoadThread(DatabaseObjects dbInfo, DatabaseInfoBase Obj, SqlCompareOptions sqloption, bool onlyExists, SelectedDatabase selectedDatabase) 
            : base()
        {
            this._dbInfo = dbInfo;
            this.dbObjects = Obj;
            this._compareOptions = sqloption;
            this._onlyExists = onlyExists;
        
            this._dbInfo.OnStart += new DatabaseObjects.DbItemLoadStateEventHandler(this.LoadStartHandler);
            this._dbInfo.OnEnd += new DatabaseObjects.DbItemLoadStateEventHandler(this.LoadEndHandler);
            this._dbInfo.OnItemLoadStart += new DatabaseObjects.DbItemLoadEventHandler(this.ItemLoadStartHandler);
            this._dbInfo.OnItemLoadEnd += new DatabaseObjects.DbItemLoadEventHandler(this.ItemLoadEndHandler);
            this.selectedDatabase = selectedDatabase;
        }

        #endregion Constructors

        #region IWorkerThread Members

        /// <summary>
        /// The start.
        /// </summary>
        public override void Start() 
        {
            Thread.Sleep(200);
            this._dbInfo.Init(this.dbObjects, this._compareOptions, this._onlyExists);
            this._dbInfo.SetDbObjects(this.selectedDatabase);
        }

        #endregion

        /// <summary>
        /// The load start handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args
        /// </param>
        private void LoadStartHandler(object sender, DatabaseItemLoadStateEventArgs e) 
        {
        }

        /// <summary>
        /// The load end handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args
        /// </param>
        private void LoadEndHandler(object sender, DatabaseItemLoadStateEventArgs e) 
        {
        }

        /// <summary>
        /// The item load start handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args
        /// </param>
        private void ItemLoadStartHandler(object sender, DatabaseItemLoadEventArgs e) 
        {
            string eventStatusId = e.ObjectType;
            Console.WriteLine("ItemLoadStartHandler");
            this.FireStartEvent(eventStatusId);
        }

        /// <summary>
        /// The item load end handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args
        /// </param>
        private void ItemLoadEndHandler(object sender, DatabaseItemLoadEventArgs e) 
        {
            string eventStatusId = e.ObjectType;
            Console.WriteLine("ItemLoadEndHandler");
            this.FireSuccessEvent(eventStatusId);
        }


        /// <summary>
        /// The fire start event.
        /// </summary>
        /// <param name="eventStatusId">
        /// The event status id.
        /// </param>
        protected virtual void FireStartEvent(string eventStatusId) 
        {
            if (this.OnStart != null)
            {
                this.OnStart(this, new WorkerThreadEventArgs(eventStatusId, EventState.Running, EventResult.None));
            }
        }

        /// <summary>
        /// The fire finish event.
        /// </summary>
        /// <param name="eventStatusId">
        /// The event status id.
        /// </param>
        protected virtual void FireFinishEvent(string eventStatusId) 
        {
            if (this.OnFinish != null)
            {
                this.OnFinish(this, new WorkerThreadEventArgs(eventStatusId, EventState.Finished, EventResult.None));
            }
        }

        /// <summary>
        /// The fire failed event.
        /// </summary>
        /// <param name="eventStatusId">
        /// The event status id.
        /// </param>
        protected virtual void FireFailedEvent(string eventStatusId) 
        {
            if (this.OnFailed != null)
            {
                this.OnFailed(this, new WorkerThreadEventArgs(eventStatusId, EventState.Finished, EventResult.Failed));
            }
        }

        /// <summary>
        /// The fire success event.
        /// </summary>
        /// <param name="eventStatusId">
        /// The event status id.
        /// </param>
        protected virtual void FireSuccessEvent(string eventStatusId) 
        {
            if (this.OnSuccess != null)
            {
                this.OnSuccess(this, new WorkerThreadEventArgs(eventStatusId, EventState.Finished, EventResult.Successfull));
            }
        }
    }
}